// pages/mywallet/wallet.js
var wallet = require('./model/WalletModel.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabInex: 0,
    tabTitleList: [
    //   {
    //   id: 0,
    //   title: '粉币充值',
    //   _selected: true
    // },
     {
      id: 1,
      title: '粉币消费',
      _selected: false
    }
    ],
    xiaofeilist: [],
    congzhilist: [],
    xiaofeilist_pageIndex: 1,
    congzhilist_pageIndex: 1,
    wallet: {},
    _reLoadWalletFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadWalletInfo()
    this.loadCongZhiData(this.data.congzhilist_pageIndex)
  },
  loadWalletInfo() {
    var that = this
    wallet.getWalletInfo(function(data) {
      wx.stopPullDownRefresh()
      that.setData({
        wallet: data,
        _reLoadWalletFlag: false
      })
    })
  },
  //加载消费列表
  loadXiaoFeiData(pageIndex) {
    var that = this
    wallet.getXiaoFeiList(pageIndex, function(list) {
      if (list != null && list.length > 0) {
        that.setData({
          xiaofeilist: that.data.xiaofeilist.concat(list),
          xiaofeilist_pageIndex: pageIndex
        })
      }
    })
  },
  //家长充值列表
  loadCongZhiData(pageIndex) {
    var that = this
    wallet.getCongZhiList(pageIndex, function(list) {
      if (list != null && list.length > 0) {
        that.setData({
          congzhilist: that.data.congzhilist.concat(list),
          congzhilist_pageIndex: pageIndex
        })
      }
    })
  },
  //tab切换
  switchTab: function(event) {
    var _index = event.currentTarget.dataset.index
    var tabs = this.data.tabTitleList
    tabs.forEach(function(item, index) {
      if (item.id == _index) {
        item._selected = true
      } else {
        item._selected = false
      }
    })
    this.setData({
      tabInex: _index,
      tabTitleList: tabs,
      xiaofeilist: [],
      congzhilist: []
    })
    if (this.data.tabInex == 0) { //充值
      var pageIndex = this.data.congzhilist_pageIndex
      this.loadCongZhiData(pageIndex)
    } else if (this.data.tabInex == 1) { //消费
      var pageIndex = this.data.xiaofeilist_pageIndex
      this.loadXiaoFeiData(pageIndex)
    }
  },
  //提现
  goWithdrawal: function(event) {
    if (Number(this.data.wallet.walletWithdrawal) <= 0) {
      wx.showToast({
        title: '提现余额不足！',
        icon: 'none',
        duration: 1500
      })
      return
    }
    wx.navigateTo({
      url: '../withdrawal/withdrawal?balance=' + this.data.wallet.walletWithdrawal,
    })
  },
  //去充值
  reChange: function(event) {
    wx.navigateTo({
      url: '../recharge/recharge',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data._reLoadWalletFlag) {
      this.loadWalletInfo()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadWalletInfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.tabInex == 0) { //充值
      var pageIndex = this.data.congzhilist_pageIndex + 1
      this.loadCongZhiData(pageIndex)
    } else if (this.data.tabInex == 1) { //消费
      var pageIndex = this.data.xiaofeilist_pageIndex + 1
      this.loadXiaoFeiData(pageIndex)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})